package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDao goodsDao;
    @Override
    public List<Goods> getPage(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        // 计算start 起始行
        Integer start = (page - 1) * rows;
        List<Goods> list = goodsDao.getAllByPage(start, rows, codeOrName, goodsTypeId);
        return list;
    }

    @Override
    public List<Goods> getComplexPage(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        // 计算start 起始行
        Integer start = (page - 1) * rows;
        return goodsDao.getComplexPage(start,rows,codeOrName,goodsTypeId);
    }

    @Override
    public void save(Goods goods) {
        log.info("------商品内容------");
        log.info(goods.toString());
        if(goods.getGoodsId() != null){
            goodsDao.update(goods);
        }else{
            goodsDao.insert(goods);
        }
    }

    @Override
    public void delete(Integer goodsId) {
        goodsDao.delete(goodsId);
    }

    /**
     * 无库存商品查询
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @Override
    public List<Goods> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Integer start = (page - 1) * rows;
        return goodsDao.getNoInventoryQuantity(start,rows,nameOrCode);
    }

    /**
     * 有库存商品列表展示（可以根据商品名称或编码查询）
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @Override
    public List<Goods> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Integer start = (page - 1) * rows;
        return goodsDao.getHasInventoryQuantity(start,rows,nameOrCode);
    }

    /**
     * 添加库存、修改数量或成本价
     * @param goodsId
     * @param inventoryQuantity
     * @param purchasingPrice
     */
    @Override
    public void saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        //查询当前价格记录为上一次的价格
        double lastPurchasingPrice = goodsDao.getById(goodsId).getPurchasingPrice();
        Goods goods = new Goods();
        goods.setGoodsId(goodsId);
        goods.setInventoryQuantity(inventoryQuantity);
        goods.setPurchasingPrice(purchasingPrice);
        goods.setLastPurchasingPrice(lastPurchasingPrice);
        log.info("-------------要修改的商品库存--------");
        log.info(goods.toString());
        goodsDao.saveStock(goods);
    }

    /**
     * 删除库存（要判断商品状态 入库、有进货和销售单据的不能删除）
     * @param goodsId
     * @return
     */
    @Override
    public ServiceVO deleteStock(Integer goodsId) {
        Goods goods = goodsDao.getById(goodsId);
        if(goods.getState()!=0){
            return new ServiceVO(100,"删除失败，该商品已经入库或有进货和销售单据");
        }else{
            goodsDao.deleteStock(goodsId);
            return new ServiceVO(100,"请求成功");
        }
    }

    @Override
    public Integer getAll() {
        return goodsDao.getTotal();
    }

    @Override
    public List<Goods> listAlarm() {
        return goodsDao.listAlarm();
    }

}
